<%@page import="fr.upmc.bean.PhoneNumber"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
//is the user allowed to be here?
if(!fr.upmc.utils.SessionTracker.isUserConnected(request))
{
	fr.upmc.utils.SessionTracker.redirectToHome(response);
	return;
}

org.hibernate.Session s = fr.upmc.utils.HibernateUtils.getSessionFactory().openSession();
long id = -1;
try{
	id = Long.parseLong(request.getParameter("id"));
}
catch(Exception e){}

fr.upmc.dao.ContactDAO dao = new fr.upmc.dao.ContactDAO(s);
fr.upmc.bean.Contact contact;

contact = dao.read(id);

/*if(contact == null){
	response.sendRedirect("accueil.jsp");
	return;
}*/
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="scripts/login/css/style.css" />
    <script src="scripts/jquery/js/jquery-1.6.2.min.js"></script>
    <script src="scripts/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
    <script src="scripts/customInput.jquery.js"></script>
    <script src="scripts/login/js/login.js"></script>
    <script src="scripts/jquery/jquery.validate.js"></script>
    <link rel="stylesheet" href="scripts/jquery/css/ui-darkness/jquery-ui-1.8.16.custom.css" />
    <link rel="stylesheet" href="scripts/customInput.css" />
	<title>Update contact</title>
	
<script type="text/javascript">
$(document).ready(function(){
    var button = $('#logoutButton');
    var form = $('#logoutForm');
    
    button.click(function(){
    	if(confirm('Are you sure you want to logout?'))
    		form.submit();
    	else
    		return;
    });
    
    $('input:submit').button();
    $('input').customInput();
    
    $("#updateContactForm").validate();
    
 	$('#updateContactSubmit').click(function(){
    	
    	if($("#updateContactForm").valid()){
    		var q = "contactId=" + "<%=id%>" +
    				"&firstname=" + $('input#firstname').val() +
    				"&lastname="+ $('input#lastname').val() +
    				"&email="+ $('input#email').val() +
    				"&street="+ $('input#street').val() +
    				"&city="+ $('input#city').val() +
    				"&zip="+ $('input#zip').val() +
    				"&country="+ $('input#country').val();
    				/*"&pn1="+ $('input#pn1').val() +
    				"&pn2="+ $('input#pn2').val() +
    				"&pn3="+ $('input#pn3').val() +
    				"&gc="+ $('input#gc').val();*/
    		
    		$("#dialog-modal").html('<p><img src="scripts/ajax-loader.gif"/></p>');
    		setTimeout(function(){$.ajax({  
    			  type: "POST",  
    			  url: "updateContact.do",  
    			  data: q,  
    			  success: function(data) {
    				  if(data == "success"){
	    				  $( "#dialog-modal" ).text("Contact updated with success, redirecting in 2 seconds..");
	    				  setTimeout(function(){
	    					  window.location="accueil.jsp";
	    					}, 2000);
    				  }
    				  else{
    					  	$( "#dialog-modal" ).fadeIn('slow');
    				  		$( "#dialog-modal" ).html("<h3>An error accured, please check your data again</h3>");
    				  }
    			  }
    			})}, 1000);
    		
    		$("#dialog-modal").dialog({
    			height: 140,
    			modal: true
    		});
    			
    	}
    	
    	return false;  
    });
});
</script>
</head>
<body>
<div id="bar">
        <div id="container">
            <!-- Login Starts Here -->
            <div id="title">MDOC Project - Update Contact</div>
            <div id="loginContainer">
            	<a href="accueil.jsp" id="home">Home</a>
                <a href="#" id="logoutButton"><span>Log out</span></a>
                <div style="clear:both"></div>
                <div id="loginBox">                
                    <form id="logoutForm" action="authentification.do" method="post">
                        <fieldset id="body">
                            <input type="hidden" name="action" value="logout" >
                            <input type="submit" id="logout" value="Sign out" />
                        </fieldset>
                    </form>
                </div>
            </div>
            <!-- Login Ends Here -->
		</div>
	</div>
	
<div class="container">
	<br/>
	<%if(contact == null){%>
		Please <b><a href="searchContact.jsp">search</a></b> for a contact to update first.
	<%}else{%>
	
	<form id="updateContactForm" action="updateContact.do" method="post">
		<fieldset>
			<legend>Basic information</legend>
			First name&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="firstname" id="firstname" size="30" class="required" minlength="2" value="<%=contact.getFirstName() %>" /><em>*</em><br/>
			Last name&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="lastname" id="lastname" size="30" class="required" minlength="2" value="<%=contact.getLastName() %>"/><em>*</em><br/>
			Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="email" id="email" size="30" class="required email" value="<%=contact.getEmail() %>"/><em>*</em>
		</fieldset>
		<br/>
		<fieldset>
			<legend>Address</legend>
			Street&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="street" id="street" size ="30" class="required" minlength="10" value="<%=contact.getAddress().getStreet()%>"/><em>*</em><br/>
			City&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="city" id="city" size="30" class="required" minlength="2" value="<%=contact.getAddress().getCity() %>"/><em>*</em><br/>
			Zip&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="zip" id="zip" size="10"  class="required zipcode" minlength="5" value="<%=contact.getAddress().getZip() %>"/><em>*</em><br/>
			Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type="text" name="country" id="country" size="30" class="required" minlength="2" value="<%=contact.getAddress().getCountry() %>"/><em>*</em>
		</fieldset>
		<br/>
		
		<br/>
		<input type="submit" id="updateContactSubmit" name="Validate" value="Update"/>
	</form>
	
	<%} %>
	
	<div id="dialog-modal" title="Updating contact ..">
		<p></p>
	</div>
</div>
</body>
</html>